package org.example.traffic.organization;

import org.example.traffic.organization.Organization;
import org.example.traffic.videoInfo.Video;
import org.example.traffic.organization.OrganizationService;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @author: wyk
 */
@RestController
@RequestMapping("/organizations")
public class OrganizationController {
    private final OrganizationService organizationService;

    public OrganizationController(OrganizationService organizationService) {
        this.organizationService = organizationService;
    }

    // 创建机构
    @PostMapping
    public ResponseEntity<Organization> createOrganization(@RequestBody Organization organization) {
        Organization savedOrganization = organizationService.saveOrganization(organization);
        return new ResponseEntity<>(savedOrganization, HttpStatus.CREATED);
    }

    // 根据ID获取机构
    @GetMapping("/{id}")
    public ResponseEntity<Organization> getOrganizationById(@PathVariable Integer id) {
        Organization organization = organizationService.getOrganizationById(id);
        if (organization!= null) {
            return new ResponseEntity<>(organization, HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    // 获取所有机构
//    @GetMapping
//    public ResponseEntity<List<Organization>> getOrganizations() {
//        List<Organization> organizations = organizationService.getAllOrganizations();
//        return new ResponseEntity<>(organizations, HttpStatus.OK);
//    }

    // 更新机构
    @PutMapping("/{id}")
    public ResponseEntity<Organization> updateOrganization(@PathVariable Integer id, @RequestBody Organization organization) {
        organization.setId(id);
        Organization updatedOrganization = organizationService.updateOrganization(organization);
        return new ResponseEntity<>(updatedOrganization, HttpStatus.OK);
    }

    // 删除机构
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteOrganizationById(@PathVariable Integer id) {
        organizationService.deleteOrganizationById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    // 异常处理方法，处理服务层抛出的用户名已存在的异常
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException e) {
        return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
    }

    // 分页查询视频
    @GetMapping
    public ResponseEntity<Page<Organization>> getOrganizations(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {
        Page<Organization> organizations = organizationService.getOrganizations(page-1, size);
        return ResponseEntity.ok(organizations);
    }
    // 根据名称模糊查询
    @GetMapping("/search")
    public ResponseEntity<Page<Organization>> getOrganizationsByName(
            @RequestParam String name,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {
        Page<Organization> organizations = organizationService.getOrganizationsByName(name, page-1, size);
        return ResponseEntity.ok(organizations);
    }
}
